import json
import tornado.websocket
import tornado.web
import tornado.ioloop
class ChatSocket(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message(json.dumps({
            'from': 'sys',
            'message': u'欢迎来到 聊天室'
        }))

    def on_close(self):
        self.write_message(json.dumps({
            'from': 'sys',
            'message': 'close'
        }))

    def on_message(self, message):
        self.write_message(json.dumps({
            'from': 'on_message',
            'message': message
        }))

    def check_origin(self, origin):
        return True

if __name__ == '__main__':
    app = tornado.web.Application([
        (r'/', ChatSocket),
    ])
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()